const Controller = require('egg').Controller

class HomeController extends Controller {
    async index() {
        // this.ctx.body = "前台系统api接口"
        // let result = await this.app.mysql.get("blog_content", {})
        // console.log(result)1
        // this.ctx.body = result
    }
    async getArticleList() {
        let id = this.ctx.params.id
        // console.log('getArticleList==', this.ctx)
        if (id) {
            console.log("getArticleList==id存在",id)
            let sql = /*sql*/`SELECT article.id as id,
                        article.title as title,
                        article.introduce as introduce,
                        FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime,
                        article.view_count as view_count,
                        type.typeName as typeName 
                        FROM article LEFT JOIN type ON article.type_id = type.id 
                        WHERE type.id=${id}`;
            const result = await this.app.mysql.query(sql)
            this.ctx.body = {
                data: result
            }
        } else {
            console.log("getArticleList==id不存在")
            let sql = /*sql*/`SELECT article.id as id,
                        article.title as title,
                        article.introduce as introduce,
                        FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime,
                        article.view_count as view_count,
                        type.typeName as typeName 
                        FROM article LEFT JOIN type ON article.type_id = type.id`;
            const result = await this.app.mysql.query(sql)
            this.ctx.body = {
                data: result
            }
        }
    }
    // 详情页数据
    async getArticleById() {
        console.log('getArticleById==', this.ctx)
        //先配置路由的动态传值，然后再接收值
        let id = this.ctx.params.id
        let sql = /*sql*/`SELECT article.id as id,
        article.title as title,
        article.introduce as introduce,
        article.article_content as article_content,
        FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime,
        article.view_count as view_count ,
        type.typeName as typeName ,
        type.id as typeId 
        FROM article LEFT JOIN type ON article.type_id = type.id
        WHERE article.id=${id}`
        if (id) {
            const result = await this.app.mysql.query(sql)
            this.ctx.body = { data: result }
        } else {
            return
        }
    }

    //得到类别名称和编号
    async getTypeInfo() {
        const result = await this.app.mysql.select('type')
        this.ctx.body = { data: result }

    }


    //根据类别ID获得文章列表
    async getListById() {
        let id = this.ctx.params.id
        let sql = /*sql*/`SELECT article.id as id,
            article.title as title,
            article.introduce as introduce,
            FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime,
            article.view_count as view_count ,
            type.typeName as typeName 
            FROM article LEFT JOIN type ON article.type_id = type.Id 
            WHERE type_id=${id}`
        const result = await this.app.mysql.query(sql)
        this.ctx.body = { data: result }
        console.log("getListById--zx")

    }
}

module.exports = HomeController


